<template>
	<view class="container">
		<view class="group-box">
			<view class="form-uni-list">
				<uni-forms :value="model" :rules="rules" ref="form">
					<uni-forms-item label="昵称" name="realName" class="list-box">
						<uni-easyinput type="text" v-model="model.realName" placeholder="请输入会员昵称" />
					</uni-forms-item>
					<uni-forms-item label="性别" name="sex" class="list-box">
						<uni-data-checkbox mode="tag" v-model="model.sex" :localdata="sexRange"></uni-data-checkbox>
					</uni-forms-item>
					<uni-forms-item label="生日" name="birthday" class="list-box">
						<uni-datetime-picker type="date" v-model="model.birthday" placeholder="请选择生日"></uni-datetime-picker>
					</uni-forms-item>
					<view class="form-foot">
						<button @click="submitForm" class="btn submit">确认提交</button>
					</view>
				</uni-forms>
			</view>
			
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				sexRange: [
					{text:'男',value:'男'},
					{text:'女',value:'女'},
					{text:'保密',value:'保密'}
				],
				model: {
					avatar: null,
					realName: null,
					sex: null,
					birthday: null
				},
				rules: {
					realName: {
						rules: [
							{
								required: true,
								errorMessage: '请输入会员昵称',
							}
						]
					},
					sex: {
						rules: [
							{
								required: true,
								errorMessage: '请选择性别',
							}
						]
					},
					birthday: {
						rules: [
							{
								required: true,
								errorMessage: '请选择出生年月日',
							},
						]
					},
				},
				
			}
		},
		methods: {
			//初始化数据
			async initData(){
				let _this = this;
				//正在加载
				uni.showLoading({
				    title: '加载中'
				});
				//获取会员信息
				await _this.$api.request({
					url: "/account/member",
				}).then((res) => {
					_this.model = res.data;
				});
				//关闭加载
				uni.hideLoading();
			},
			//提交表单
			submitForm() {
				let _this = this;
				_this.$refs.form.validate().then(async res=>{
					await this.$api.request({
						url: `/account/member/info`,
						method: "put",
						data: _this.model,
						load: true
					}).then(res=>{
						_this.$api.msg(`更新成功`,1500,false,'success');
						setTimeout(()=>{
							_this.initData();
						}, 1000);
						
					});
				}).catch(err =>{
					//console.log('表单错误信息：', err);
				});
			}
		},
		//页面初始化
		onLoad(options){
			this.initData();
		},
	}
</script>
